const UPNG = require('./UPNG')
import base from './base64util.js'
Page({
  data: {
    showCamera: false,
    username: '',
    pwd: '',
    base64:''
  },
  onInputUsername: function (e) {
    this.setData({
      username: e.detail.value
    });
  },
  onInputPassword: function (e) {
    this.setData({
      pwd: e.detail.value
    });
  },
  onLogin: function () {
    wx.request({
      url: 'http://192.168.2.18:9999/zgyd-user/login?username='+this.data.username+"&pwd="+this.data.pwd,
      method: "POST",
      success: function (res) {
        console.log(res.data);
        wx.setStorage({ key: 'token', data: res.data.data });
        if (res.data.code === 200) {
          wx.switchTab({
            url: '/pages/index/index'
          })
        } else {
          wx.showToast({
            title: '登录失败',
            icon: 'error',
            duration: 1000
          })
        }
      },
      fail: function (error) {
        console.log(error);
      }
    })
  },
  faceLogin:function(){
    const ctx = wx.createCameraContext()
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        wx.getFileSystemManager().readFile(
          {
            filePath: res.tempImagePath,
            encoding: 'base64',
            success:(res)=> {
             //上传到服务器匹配
             wx.request({
               url: 'https://aip.baidubce.com/rest/2.0/face/v3/search?access_token=24.6198563a7bacc7b5b4a47a5139e563d2.2592000.1719370748.282335-75309502',
               method:"POST",
               data:{
                 "group_id_list":"user",
                 "image":res.data,
                 "image_type":"BASE64"
               },
               success:(res)=>{
                 console.log("成功",res);
                 let resdata = res.data;
                 if (null!=resdata.result.user_list&&resdata.result.user_list[0].score>90) {
                   //更具user_id去找数据库是否有该用户
                   wx.request({
                     url: 'http://192.168.2.18:9999/zgyd-user/faceLogin?userId='+resdata.result.user_list[0].user_id,
                     method:"POST",
                     success:(res)=>{
                      //成功将token存入storage
                      console.log("成功",res.data.data);
                      wx.setStorage({"key":"token","data":res.data.data})
                      wx.switchTab({
                        url: '/pages/index/index'
                      })
                      wx.showToast({
                        title: '登录成功',
                        icon: 'success',
                        duration: 1000
                      })
                     },
                     fail:(res)=>{
                       console.log("登录失败");                    
                     }
                   })
                 }else{
                  wx.reLaunch({
                    url: '/pages/loginPage/loginPage', // 替换为你要跳转的页面路径
                  });
                  wx.showToast({
                    title: '登录失败',
                    icon:"error",
                    duration: 1000
                  })
                  console.log("登陆失败",96);         
                 }
               },
               fail:(res)=>{
                 wx.showToast({
                   title: '登录失败',
                   image_type:"error"
                 })
                 console.log("登录失败",113);
                 wx.pageScrollTo({
                  scrollTop: 0,
                  duration: 0
                })              
               }
             })
              console.log(res.data);
            //  this.setData({
            //    base64:'data:image/png;base64,'+res.data
            //  })
            }
          })
        }
      })
      return 'success';
  },
  onFaceLogin: function () {
    this.setData({
      showCamera: true,
    });
    console.log('开始刷脸登录');
    setInterval(()=>{
    this.faceLogin();
    },8000)
  },
});
